<!DOCTYPE html>
<html>
<head>
    <title>HBr Infrared Absorption Spectrum</title>
</head>
<body>
<canvas id="physicsCanvas" width="800" height="500"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Define drawing area and margins
    const margin = { top: 70, right: 30, bottom: 90, left: 80 };
    const plotWidth = canvas.width - margin.left - margin.right;
    const plotHeight = canvas.height - margin.top - margin.bottom;

    // --- Coordinate transformation functions ---
    // Maps data coordinates to canvas coordinates
    function xVal(wavenumber) {
        const minWavenumber = 2300;
        const maxWavenumber = 2700;
        return margin.left + plotWidth * (wavenumber - minWavenumber) / (maxWavenumber - minWavenumber);
    }

    function yVal(absorption) {
        const minAbsorption = 20;
        const maxAbsorption = 100;
        // Add a little padding at the top and bottom
        const plotMinY = minAbsorption - 10;
        const plotMaxY = maxAbsorption + 10;
        return margin.top + plotHeight * (1 - (absorption - plotMinY) / (plotMaxY - plotMinY));
    }

    // --- Drawing Styles ---
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 2;
    ctx.font = '20px Arial';

    // --- Draw Plot Box ---
    ctx.strokeRect(margin.left, margin.top, plotWidth, plotHeight);

    // --- Draw Y-Axis Ticks and Labels ---
    const yTicks = [20, 40, 60, 80, 100];
    ctx.textAlign = 'right';
    ctx.textBaseline = 'middle';
    yTicks.forEach(tick => {
        const y = yVal(tick);
        ctx.beginPath();
        ctx.moveTo(margin.left - 5, y);
        ctx.lineTo(margin.left, y);
        ctx.stroke();
        ctx.fillText(tick, margin.left - 10, y);
    });

    // --- Draw Y-Axis Title ---
    ctx.save();
    ctx.translate(margin.left - 55, margin.top + plotHeight / 2);
    ctx.rotate(-Math.PI / 2);
    ctx.textAlign = 'center';
    ctx.fillText('Absorption (%)', 0, 0);
    ctx.restore();

    // --- Draw X-Axis Ticks and Labels ---
    const xWavenumberTicks = [2300, 2400, 2500, 2600, 2700];
    const xFrequencyValues = ['6.9', '7.2', '7.5', '7.8', '8.1'];

    ctx.textAlign = 'center';
    ctx.textBaseline = 'top';

    xWavenumberTicks.forEach((tick, i) => {
        const x = xVal(tick);

        // Bottom Ticks (Wavenumber)
        ctx.beginPath();
        ctx.moveTo(x, margin.top + plotHeight);
        ctx.lineTo(x, margin.top + plotHeight + 5);
        ctx.stroke();
        ctx.fillText(tick, x, margin.top + plotHeight + 10);

        // Top Ticks (Frequency)
        ctx.beginPath();
        ctx.moveTo(x, margin.top);
        ctx.lineTo(x, margin.top - 5);
        ctx.stroke();

        // Frequency Labels (e.g., 8.1 x 10^13)
        const baseText = `${xFrequencyValues[i]} \u00D7 10`;
        const supText = '13';
        
        const originalFont = ctx.font;
        ctx.font = '18px Arial';
        const baseWidth = ctx.measureText(baseText).width;
        ctx.font = '12px Arial';
        const supWidth = ctx.measureText(supText).width;

        const totalWidth = baseWidth + supWidth;
        const startX = x - totalWidth / 2;

        ctx.font = '18px Arial';
        ctx.textAlign = 'left';
        ctx.textBaseline = 'alphabetic';
        ctx.fillText(baseText, startX, margin.top - 20);
        
        ctx.font = '12px Arial';
        ctx.fillText(supText, startX + baseWidth, margin.top - 20 - 8);
        
        ctx.font = originalFont; // Restore font
    });

    // --- Draw X-Axis Titles ---
    ctx.textAlign = 'center';
    ctx.textBaseline = 'top';

    // Top Title: ν (Hz)
    ctx.fillText('ν (Hz)', xVal(2700) - 40, margin.top - 55);

    // Bottom Title: ν̃ (cm⁻¹)
    const wavenumberLabel = 'ν̃ (cm⁻¹)';
    const xWavenumberLabel = xVal(2700) - 60;
    const yWavenumberLabel = margin.top + plotHeight + 40;
    ctx.fillText(wavenumberLabel, xWavenumberLabel, yWavenumberLabel);

    // Manually draw the tilde over ν for the bottom label
    const nuWidth = ctx.measureText('ν').width;
    const preTextWidth = ctx.measureText('ν').width / 2; // Position tilde over ν
    const tildeX = xWavenumberLabel - ctx.measureText(wavenumberLabel).width / 2 + preTextWidth;
    const tildeY = yWavenumberLabel + 2;
    ctx.beginPath();
    ctx.moveTo(tildeX - 4, tildeY);
    ctx.quadraticCurveTo(tildeX, tildeY - 4, tildeX + 4, tildeY);
    ctx.lineWidth = 1.5;
    ctx.stroke();
    ctx.lineWidth = 2; // Reset line width

    // --- Draw Figure Caption ---
    ctx.font = '18px Arial';
    ctx.fillText('Fig. 1.66', canvas.width / 2, canvas.height - 25);


    // --- Draw the Spectrum Data ---
    const spectrumData = [
        [2300, 96.0], [2305.0, 97.0],
        [2308.0, 97.0], [2310.5, 80.0], [2313.0, 97.0],
        [2320.0, 96.5],
        [2333.0, 96.0], [2335.5, 70.0], [2338.0, 96.0],
        [2348.0, 97.0],
        [2358.0, 97.5], [2360.5, 60.0], [2363.0, 97.5],
        [2373.0, 98.0],
        [2383.0, 98.0], [2385.5, 50.0], [2388.0, 98.0],
        [2398.0, 97.5],
        [2408.0, 97.0], [2410.5, 40.0], [2413.0, 97.0],
        [2423.0, 96.5],
        [2433.0, 96.0], [2435.5, 30.0], [2438.0, 96.0],
        [2448.0, 96.5],
        [2457.0, 97.0], [2459.5, 25.0], [2462.0, 97.0],
        [2470.0, 96.5],
        [2480.0, 96.0], [2482.5, 22.0], [2485.0, 96.0],
        [2492.0, 96.5],
        [2501.0, 97.0], [2503.5, 28.0], [2506.0, 97.0],
        [2513.0, 96.5],
        [2521.0, 96.0], [2523.5, 38.0], [2526.0, 96.0],
        [2533.0, 95.5],
        [2541.0, 95.0], [2543.5, 55.0], [2546.0, 95.0],
        [2555.0, 94.0], [2565.0, 94.5],
        [2577.0, 95.0], [2579.5, 40.0], [2582.0, 95.0],
        [2590.0, 95.5],
        [2597.0, 96.0], [2599.5, 30.0], [2602.0, 96.0],
        [2610.0, 96.5],
        [2617.0, 97.0], [2619.5, 25.0], [2622.0, 97.0],
        [2630.0, 96.5],
        [2637.0, 96.0], [2639.5, 22.0], [2642.0, 96.0],
        [2650.0, 96.5],
        [2657.0, 97.0], [2659.5, 25.0], [2662.0, 97.0],
        [2670.0, 96.5],
        [2677.0, 96.0], [2679.5, 35.0], [2682.0, 96.0],
        [2690.0, 96.5],
        [2697.0, 97.0], [2699.5, 45.0], [2702.0, 97.0],
        [2710.0, 96.5],
        [2717.0, 96.0], [2719.5, 60.0], [2722.0, 96.0],
        [2730.0, 95.5],
        [2732.0, 95.0], [2734.5, 80.0], [2737.0, 95.0],
        [2740.0, 94.0]
    ];

    ctx.beginPath();
    ctx.lineWidth = 3;
    ctx.lineJoin = 'round';
    ctx.lineCap = 'round';
    ctx.moveTo(xVal(spectrumData[0][0]), yVal(spectrumData[0][1]));
    for (let i = 1; i < spectrumData.length; i++) {
        ctx.lineTo(xVal(spectrumData[i][0]), yVal(spectrumData[i][1]));
    }
    ctx.stroke();

</script>
</body>
</html>